package com.cssw.fyzb.tps.util;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 字典排序，sha1 编码
 * @Author zhouxu
 * @create 2020-09-14 20:50
 */
public class SignUtil {

    private static final Logger logger = LoggerFactory.getLogger(SignUtil.class);

    public static String sign(List<String> values, String ticket) {
        if (values == null) {
            throw new NullPointerException("values is null");
        }
        values.removeAll(Collections.singleton(null));// remove null
        values.add(ticket);
        Collections.sort(values);

        StringBuilder sb = new StringBuilder();
        for (String s : values) {
            sb.append(s);
        }
        logger.info("字典排序后的拼接字符串："+sb.toString());
        return Hashing.sha1().hashString(sb, Charsets.UTF_8).toString().toUpperCase();
    }

    public static void main(String[] args){
        String s1 = "test";
        String s2 = "abc";
        List<String> values = new ArrayList<>();
        values.add(s1);
        values.add(s2);
        printList(values);
        Collections.sort(values);
        printList(values);


    }

    private static void printList(List<String> values){
        for (String s: values){
            System.out.println(s);
        }
    }

}
